﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AMS.DataAccess.Trademark.Filters;
using AMS.DataAccess.Filter;
using AMS.DataAccess;
using AMS.DataAccess.Trademark.BO.Enums;

public partial class Task_ucSearchTask : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (txtSearchName.Text == "输入查询名称")
        {
            txtSearchName.Text = "";
        }
    }

    public TaskFilter GetTaskFilter()
    {
        TaskFilter filter = new TaskFilter();
        if (!string.IsNullOrEmpty(txtCaseIndex.Text))
        {
            filter.CaseIndex = new StringFilterField(txtCaseIndex.Text);
        }
        if(!string.IsNullOrEmpty(txtTaskName.Text))
        {
            filter.TaskName = new StringFilterField(txtTaskName.Text, StringOperations.Like);
        }

        DateTime dt = DateTime.Now;
        DateTime dtCompared = DateTime.MinValue;
        if (drpStartTime.SelectedIndex > 0)
        {
            if (drpStartTime.SelectedIndex == 1)
            {
                dtCompared = dt;
            }
            else if (drpStartTime.SelectedIndex == 2)
            {
                dtCompared = dt.AddDays(-3);
            }
            else if (drpStartTime.SelectedIndex == 3)
            {
                int daysToMonday = (int)dt.DayOfWeek - (int)DayOfWeek.Monday;
                dtCompared = dt.AddDays(0 - daysToMonday);
            }
            else
            {
                dtCompared = dt.AddDays(0 - dt.Day);
            }

            filter.StartDate = new DateTimeFilterField(dtCompared, DateTimeType.Date, SqlOperations.LargerThanOrEquals);
        }

        if (!(chkHigh.Checked && chkLow.Checked && chkNormal.Checked))
        {
            if (chkHigh.Checked) filter.Priorities.Add(TaskPriority.High);
            if (chkNormal.Checked) filter.Priorities.Add(TaskPriority.Normal);
            if (chkLow.Checked) filter.Priorities.Add(TaskPriority.Low);
        }

        if(!(chkNew.Checked && chkUnCompleted.Checked && chkCompleted.Checked && chkChecked.Checked && !chkClosed.Checked))
        {
            if (chkNew.Checked) filter.StatusList.Add(TaskStatus.NewTask);
            if (chkUnCompleted.Checked) filter.StatusList.Add(TaskStatus.UnCompleted);
            if (chkCompleted.Checked) filter.StatusList.Add(TaskStatus.Completed);
            if (chkChecked.Checked) filter.StatusList.Add(TaskStatus.Checked);
            if (chkClosed.Checked)
            {
                filter.StatusList.Add(TaskStatus.Closed);
                filter.QueryClosedTask = true;
            }
        }

        return filter;
    }

    public string QueryName
    {
        get { return txtSearchName.Text; }
        set { txtSearchName.Text = value; }
    }

    public event EventHandler<EventArgs<TaskFilter>> FilterTask;
    protected void OnTaskFiltered(TaskFilter filter)
    {
        if (FilterTask != null)
        {
            FilterTask(this, new EventArgs<TaskFilter>(txtSearchName.Text, filter));
        }
    }
    protected void btnOK_Click(object sender, EventArgs e)
    {
        TaskFilter filter = GetTaskFilter();
        OnTaskFiltered(filter);
    }
}
